//
//  UICollectionReusableView+BNAdd.m
//  BNCodeGuide
//
//  Created by kevin007 on 2021/5/20.
//  Copyright © 2021 kevin007. All rights reserved.
//

#import "UICollectionReusableView+BNAdd.h"

@implementation UICollectionReusableView (BNAdd)
#pragma mark - ----------------Public Methods----------------
/// headerReusableViewFromXib
/// @param collectionView UICollectionView
/// @param indexPath indexPath NSIndexPath
+ (instancetype)headerReusableViewFromXibWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath {
    return [self reusableSupplementaryViewFromXibOfKind:UICollectionElementKindSectionHeader collectionView:collectionView forIndexPath:indexPath];
}

/// headerReusableViewFromClass
/// @param collectionView UICollectionView
/// @param indexPath indexPath NSIndexPath
+ (instancetype)headerReusableViewFromClassWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath {
    return [self reusableSupplementaryViewFromClassOfKind:UICollectionElementKindSectionHeader collectionView:collectionView forIndexPath:indexPath];
}

/// footerReusableViewFromXib
/// @param collectionView UICollectionView
/// @param indexPath indexPath NSIndexPath
+ (instancetype)footerReusableViewFromXibWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath {
    return [self reusableSupplementaryViewFromXibOfKind:UICollectionElementKindSectionFooter collectionView:collectionView forIndexPath:indexPath];
}

/// footerReusableViewFromClass
/// @param collectionView UICollectionView
/// @param indexPath indexPath NSIndexPath
+ (instancetype)footerReusableViewFromClassWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath {
    return [self reusableSupplementaryViewFromClassOfKind:UICollectionElementKindSectionFooter collectionView:collectionView forIndexPath:indexPath];
}

#pragma mark - ----------------Private Methods----------------
+ (instancetype)reusableSupplementaryViewFromXibOfKind:(NSString *)elementKind collectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = [self.reusedId stringByAppendingString:elementKind];
    NSMutableSet *identifierSet = [self setReusableSupplementaryViewIdentifierSetForCollectionView:collectionView];
    
    if (![identifierSet containsObject:identifier]) {
        NSLog(@"register nib supplementary view");
        [collectionView registerNib:[UINib nibWithNibName:self.reusedId bundle:nil] forSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier];
        [identifierSet addObject:identifier];
    }
    
    return [collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier forIndexPath:indexPath];
}

+ (instancetype)reusableSupplementaryViewFromClassOfKind:(NSString *)elementKind collectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = [self.reusedId stringByAppendingString:elementKind];;
    NSMutableSet *identifierSet = [self setReusableSupplementaryViewIdentifierSetForCollectionView:collectionView];
    
    if (![identifierSet containsObject:identifier]) {
        NSLog(@"register class supplementary view");
        [collectionView registerClass:[self class] forSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier];
        [identifierSet addObject:identifier];
    }
    
    return [collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier forIndexPath:indexPath];
    
}

/** 关联 IdentifierSet*/
+ (NSMutableSet *)setReusableSupplementaryViewIdentifierSetForCollectionView:(UICollectionView *)collectionView {
    if (![collectionView getAssociatedValueForKey:_cmd]) {
        NSMutableSet *identifierSet = [[NSMutableSet alloc] init];
        [collectionView setAssociateValue:identifierSet withKey:_cmd];
        return identifierSet;
    }
    
    return [collectionView getAssociatedValueForKey:_cmd];
}
@end
